package data.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


import data.dao.db.DBTruckDAO;
import data.dao.db.DBEventDAO;


public class DBManager {
    private static final Logger LOGGER = Logger.getLogger(DBManager.class.getName());
    
    public static DBManager INSTANCE = new DBManager();

    private boolean isGoogleAppEngine;
    private ITruckDAO truckDAO = null;
    private IEventDAO eventDAO = null;
    //private INeedDAO needDAO = null;
    
    private Connection connection;
    
    public DBManager() {
	//String sPathToDB = WebAppInitializer.ROOT + "db/tasks.sqlite";
	String log = "SQlite";
	

	try {
	    System.out.println("Connection.........");
	    Class.forName("org.sqlite.JDBC");
	    System.out.println("  Driver: OK");
	    connection = DriverManager.getConnection("jdbc:sqlite:enviee.sqlite");
	    System.out.println("  DataBase: OK");
	    connection.setAutoCommit(true);
	     	
	    System.out.println("Creation de la base....");
	    executeUpdate("CREATE TABLE IF NOT EXISTS " 
			  + DBTruckDAO.TABLE_NAME + " ("
			  + DBTruckDAO.CREATE_TABLE_STATEMENT + ")");
	    executeUpdate("CREATE TABLE IF NOT EXISTS " 
			  + DBEventDAO.TABLE_NAME + " ("
			  + DBEventDAO.CREATE_TABLE_STATEMENT + ")");
	    
	    System.out.println("   Table: OK");


	} catch (SQLException e) {
	    LOGGER.log(Level.SEVERE, "Error while getting connexion", e);
	    connection = null;
	} catch (ClassNotFoundException e) {
	    LOGGER.log(Level.SEVERE, "Error while getting connexion", e);
	    connection = null;
	}
	
	
	LOGGER.log(Level.INFO, "Assuming " + log + " context");
    }
    
    public IEventDAO getEventDAO() {
	if (eventDAO == null) {
	    eventDAO = new DBEventDAO();
	}
	return eventDAO;
    }
      
    public ITruckDAO getTruckDAO() {
	if (truckDAO == null) {
	    truckDAO = new DBTruckDAO();
	}
	return truckDAO;
    }
    
    public ResultSet executeQuery(String query) {
	Statement statement = null;
	ResultSet resultSet = null;
	try {
	    statement = connection.createStatement();
	    resultSet = statement.executeQuery(query);
	} catch (SQLException e) {
	    LOGGER.log(Level.SEVERE, "Error while executing query", e);
	    resultSet = null;
	} finally {
	    try {
		if (statement != null) {
		    statement.close();
		}
	    } catch (Exception e) {
		e.printStackTrace();
		resultSet = null;
	    }
	}
	return resultSet;
    }
    
    public boolean executeUpdate(String query) {
	Statement statement = null;
	int result;
	try {
	    statement = connection.createStatement();
	    result = statement.executeUpdate(query);
	} catch (SQLException e) {
	    LOGGER.log(Level.SEVERE, "Error while executing update", e);
	    result = -1;
	} finally {
	    try {
		if (statement != null) {
		    statement.close();
		}
		
	    } catch (Exception e) {
		e.printStackTrace();
		result = -1;
	    }
	}
	return result > 0;
    }
    
    public PreparedStatement prepareStatement(String statement) {
	PreparedStatement pStatement;
	try {
	    pStatement = connection.prepareStatement(statement);
	} catch (SQLException e) {
	    LOGGER.log(Level.SEVERE, "Error while preparing statement", e);
	    pStatement = null;
	}
	return pStatement;
    }
}
